import { writeOffFeeApi } from "@/api/financialManagement/payableManagement/paymentApproval";
import { message } from "ant-design-vue";
import { computed, Ref } from "vue";
export default function OperationHook(state: {
  emit: any;
  loading: Ref<boolean>;
  formState: PaymentApproval.WriteOffFeeInfo;
  applyNoRef: Ref<any>;
  writeNoRef: Ref<any>;
  drawerOpts: Base.DrawerOpts;
  selectWriteNoRow: Ref<any>;
}) {
  const { emit, loading, applyNoRef, writeNoRef, drawerOpts, selectWriteNoRow } = state;
  const selectRowsApplyAmount = computed<string>(() => {
    if (applyNoRef.value) {
      const allRows = applyNoRef.value.getCheckboxRecords() || [];
      const allAmount = allRows.map((t: any) => t.paymentAmount || 0);
      return allAmount.reduce((a: number, b: number) => (a * 100 + b * 100) / 100, 0).toFixed(2);
    } else return 0;
  });
  const submitHandle = () => {
    const writeNo = writeNoRef.value.getRadioRecord();
    if (!writeNo) return message.error("请选择流水!");
    const selectApplyNo = applyNoRef.value.getCheckboxRecords() || [];
    if (!selectApplyNo.length) return message.error("请选择账单!");
    const params = {
      id: writeNo.id,
      applyIdList: selectApplyNo.map((t: any) => t.id)
    };
    loading.value = true;
    return writeOffFeeApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("核销成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };
  // 查看账单详情 ApprovalPending
  const showApplyInfoHandle = (row: Base.GeneraDataType) => {
    drawerOpts.title = `申请详情`;
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.passData = row;
    drawerOpts.component = "ApprovalPending";
    drawerOpts.visible = true;
  };
  const clearRadioRowEevnt = () => {
    const $table = writeNoRef.value;
    if ($table) {
      selectWriteNoRow.value = null;
      $table.clearRadioRow();
    }
  };
  return {
    clearRadioRowEevnt,
    submitHandle,
    selectRowsApplyAmount,
    showApplyInfoHandle
  };
}
